// pages/myActiveXq/myActiveXq.js
const app = getApp()
var url = require('../../utils/url.js');
var orderId
var token;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    listItem: [],
    adrInfo:"",
    row:"",
    is_award:"",
    award:"",
    addressId: "",
    bill:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    orderId=options.id;
    var that=this;
    wx.getStorage({
      key: 'token',
      success: function(res) {
        token=res.data;
        that.getInfo(token);
      },
    })

  },
  getInfo(token){
    var that=this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/treasure/info',
      data: {id:orderId },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        var msg=res.data.msg
        if(res.data.code==200){
          var goods_salenum = res.data.data.row.goods_salenum;
          var goods_storage = res.data.data.row.goods.goods_storage;
          var bill = (goods_salenum / goods_storage) * 100 ;
          var billF = bill.toFixed(2) + "%"
          that.setData({
            row: res.data.data.row,
            is_award: res.data.data.is_award,
            award: res.data.data.award,
            adrInfo: res.data.data.row.address,
            bill: billF
          });
        }else{
          
        }
        
      }
    });
  },
  toGood(){
    var that=this;
    var row = that.data.row;
    var item = JSON.stringify(that.data.award) 
    if (row.status<3){
      wx.navigateTo({
        url: '/pages/jfDetails/jfDetails?goodsId='+row.id,
      })
    }else{
      wx.navigateTo({
        url: '/pages/zuixinDetail/zuixin?goodsId=' + row.id + '&item=' + item,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    var addressId = that.data.addressId
    if (addressId != 0) {
      wx.getStorage({
        key: 'token',
        success: function (res) {
          token = res.data
          that.getDefault()
        },
      })
    }
  },
  queAdr: function (e) {
    orderId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/myadr/myadr?type=2&orderId=' + orderId,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  getDefault: function (e) {
    var that = this;
    var addressId = that.data.addressId
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/treasure/confirm',
      data: { id: orderId, address_id: addressId },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
        if (res.data.code == 200) {
          that.getlist()
        }
      }
    })
  },
  toShaidan: function (e) {
    var that = this;
    var item = e.currentTarget.dataset.item;
    var JsonItem = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/toShaidan/toShaidan?item=' + JsonItem,
    })
  },
  refund: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item;
    var jsonItem = JSON.stringify(item)
    wx.navigateTo({
      url: '/pages/myAfterSale/myAfterSale?item=' + jsonItem,
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  queOrder: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中..',
      mask: true,
    })
    wx.request({
      url: url.URl + 'my/treasure/confirm/order',
      data: { id: orderId },
      method: 'post',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        wx.showToast({
          title: msg,
          icon: 'none',
        })
        if (res.data.code == 200) {
          that.getInfo(token)
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})